package ve.com.ucv.changedetection.tools;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import ve.com.ucv.changedetection.objects.HashTable;
import ve.com.ucv.changedetection.objects.LookupTable;
import ve.com.ucv.changedetection.objects.SubtreeTable;
import ve.com.ucv.changedetection.objects.TemporaryTable;

import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: josmaguirre
 * Date: 1/3/13
 * Time: 10:18 AM
 * To change this template use File | Settings | File Templates.
 */
public class PrinterFactory {
    final static Logger logger = Logger.getLogger(PrinterFactory.class);

    public static void printDomTree(Node domTree, String tab){

        //System.out.println(domTree.getNodeName());
        if (domTree.hasChildNodes()){
            System.out.println(tab + domTree.getNodeName());
            for (int i = 0; i < domTree.getChildNodes().getLength(); i++){
                printDomTree(domTree.getChildNodes().item(i), "| " + tab);
            }
        }else{
            System.out.println(tab + domTree.getNodeName());
        }
    }

    public static void printHashTable(HashTable table){
        logger.info(String.format("%-10S %-10S %-10S %-60S %-10S", "ROW","ROW_ID", "NODE", "NODETYPE", "WEIGHT"));
        for (int i = 0; i < table.getHashTable().size(); i++){
            logger.info(String.format("%-10d %-10d %-10S %-60S %-10S",
                    table.getHashTable().get(i).getRow(),
                    table.getHashTable().get(i).getNodeId(),
                    table.getHashTable().get(i).getNode(),
                    table.getHashTable().get(i).getType(),
                    table.getHashTable().get(i).getWeight()
            ));
        }
        logger.info("FIN DE TABLA---------------------------------------------------------------------------------------");
    }

    public static void printHashTable(PrintWriter writer, HashTable table, String info){
        writer.println("------------ TABLA HASH " + info + "-------------");
        writer.println("ROW|ROW_ID|NODE|NODE_TYPE|WEIGHT");
        for (int i=0; i < table.getHashTable().size(); i++){
            writer.println(table.getHashTable().get(i).getRow() + "|" +
                    table.getHashTable().get(i).getNodeId() + "|" +
                    table.getHashTable().get(i).getNode() + "|" +
                    table.getHashTable().get(i).getType() + "|" +
                    table.getHashTable().get(i).getWeight());
            System.out.println(table.getHashTable().get(i).getRow() + "|" +
                    table.getHashTable().get(i).getNodeId() + "|" +
                    table.getHashTable().get(i).getNode() + "|" +
                    table.getHashTable().get(i).getType() + "|" +
                    table.getHashTable().get(i).getWeight());
        }
        writer.println("------------ FIN TABLA HASH " + info + "-------------");
    }

    public static void printSubtreeTable(SubtreeTable subtree){

        logger.info(String.format("%-10S %-10S %-10S %-10S", "INDEX", "MARK", "START_ID", "END_ID"));
        for (int i = 0; i < subtree.getSubtreeRows().size(); i++){
            logger.info(String.format("%-10d %-10S %-10S %-10S",
                    i,
                    subtree.getSubtreeRow(i).getMark(),
                    subtree.getSubtreeRow(i).getStartId(),
                    subtree.getSubtreeRow(i).getEndId()
            ));
        }
        logger.info("FIN DE TABLA---------------------------------------------------------------------------------------");
    }

    public static void printSubtreeTable(PrintWriter writer, SubtreeTable table, String info){
        writer.println("------------ TABLA SUB ARBOLES " + info + "-------------");
        writer.println("INDEX|MARK|START_ID|END_ID");
        for (int i=0; i < table.getSubtreeRows().size(); i++){
            writer.println(table.getSubtreeRow(i).getMark() + "|" +
                    table.getSubtreeRow(i).getStartId() + "|" +
                    table.getSubtreeRow(i).getEndId());
        }
        writer.println("------------ FIN TABLA HASH " + info + "-------------");
    }

    public static void printLookupTable(ArrayList<LookupTable> lookupTable){
        logger.info(String.format("%-10S %-10S", "MARK", "REFERENCES"));
        for (int i = 0; i < lookupTable.size(); i++){
            String references = "";
            //Obteniendo las referencias de cada una de las marcas
            for (int j = 0; j < lookupTable.get(i).getReferences().size(); j++){
                references += lookupTable.get(i).getReferences().get(j) + " ";
            }

            logger.info(String.format("%-10S %-10S",
                    lookupTable.get(i).getMark(),
                    references
            ));
        }
        logger.info("FIN DE TABLA---------------------------------------------------------------------------------------");

    }

    public static void printLookupTable(PrintWriter writer, ArrayList<LookupTable> lookupTable, String info){
        writer.println("------------ TABLA LOOKUP ASOCIADA " + info + "-------------");
        writer.println("MARK|REFERENCES");
        for(int i=0 ; i< lookupTable.size(); i++){
            String references = "";
            for (int j = 0; j < lookupTable.get(i).getReferences().size(); j++){
                references += lookupTable.get(i).getReferences().get(j) + " ";
            }
            writer.println(lookupTable.get(i).getMark() + "|" + references);
        }
        writer.println("------------ FIN TABLA LOOKUP ASOCIADA " + info + "-------------");
    }

    public static void printTemporaryTable(TemporaryTable temporaryTable){
        logger.info(String.format("%-10S %-10S %-10S %-10S %-10S %-10S", "NODE_ID1", "NODE_ID2", "ATTRIBUTE", "WEIGHT_P", "WEIGHT_T", "CS"));
        for (int i = 0; i < temporaryTable.getTemporaryRows().size(); i++){

            logger.info(String.format("%-10S %-10S %-10S %-10S %-10S %-10S",
                    temporaryTable.getTemporaryRows().get(i).getNodeId1(),
                    temporaryTable.getTemporaryRows().get(i).getNodeId2(),
                    temporaryTable.getTemporaryRows().get(i).getAttribute(),
                    temporaryTable.getTemporaryRows().get(i).getWeightPosition(),
                    temporaryTable.getTemporaryRows().get(i).getWeightTotal(),
                    temporaryTable.getTemporaryRows().get(i).getCS()
            ));
        }
        logger.info("FIN DE TABLA---------------------------------------------------------------------------------------");
    }

    public static void printTemporaryTable(PrintWriter writer, TemporaryTable temporaryTable, String info){
        writer.println("------------ TABLA TEMPORAL " + info + "-------------");
        writer.println("NODE_ID1|NODE_ID2|ATTRIBUTE|WEIGHT_P|WEIGHT_T|CS");
        for (int i = 0; i < temporaryTable.getTemporaryRows().size(); i++){
            writer.println(
                    temporaryTable.getTemporaryRows().get(i).getNodeId1() + "|" +
                    temporaryTable.getTemporaryRows().get(i).getNodeId2() + "|" +
                    temporaryTable.getTemporaryRows().get(i).getAttribute() + "|" +
                    temporaryTable.getTemporaryRows().get(i).getWeightPosition() + "|" +
                    temporaryTable.getTemporaryRows().get(i).getWeightTotal() + "|" +
                    temporaryTable.getTemporaryRows().get(i).getCS()
            );
        }
    }
}
